package view;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.*;


import controller.ContactsControllerListener;
import po.*;

/**
 * 联系人管理主界面
 * MVC的view类
 * @author 27261
 *
 */
public class ContactsFrame extends JFrame{
	private JTabbedPane p;
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener controllerListener;
	private JPanel tablePanel;
	private int selRow = -1;//选中的表格行索引
	private long studentId;
	private int roomId;
	private JPanel studentPanel;
	private JPanel roomPanel;
	public ContactsFrame() {
		controllerListener = new ContactsControllerListener();
		studentPanel = new JPanel();
		roomPanel = new JPanel();
		p = new JTabbedPane();
		studentPanel = getStudentTablePanel(controllerListener.getContactsService().findAllStudent());
		roomPanel = getRoomTablePanel(controllerListener.getContactsService().findAllRoom());
		this.p.add("联系人列表",studentPanel);
		this.p.add("宿舍列表",roomPanel);
		this.p.setFont(new Font("微软雅黑",Font.PLAIN,14));
		this.controllerListener = new ContactsControllerListener();
		this.controllerListener.setContactsFrame(this); //向controller注入frame，用于更新view。需要更新的组件需设为frame属性，并生成getter方法。
		this.setJMenuBar(getStudentMenuBar());
		this.add(this.getModifyToolBar(), BorderLayout.NORTH);
		this.add(p, BorderLayout.WEST);
		this.setVisible(true);
		this.setSize(300, 620);
		this.setLocationRelativeTo(null);
		this.validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
        this.validate();
	}
	/**
	 * 重新加载通讯录数据表格面板
	 */
	public void studentRefresh(List<Student> studentList) {
		System.out.println("测试——当前通讯录中共有"+studentList.size()+"位联系人");
		p.remove(studentPanel);
		studentPanel = this.getStudentTablePanel(controllerListener.getContactsService().findAllStudent());
		this.p.add("联系人列表",studentPanel);
		this.p.add("宿舍列表",roomPanel);
		this.add(p,BorderLayout.WEST);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}
	/**
	 * 重新加载宿舍表数据表格面板
	 */
	public void roomRefresh(List<Room> roomList) {
		System.out.println("测试——当前宿舍表中共有"+roomList.size()+"个宿舍");
		this.p.remove(roomPanel);
		roomPanel = this.getRoomTablePanel(controllerListener.getContactsService().findAllRoom());
		this.p.add("宿舍列表",roomPanel);
		this.add(p,BorderLayout.WEST);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}
	/**
	 * 查询结果
	 */
	public void showStudentSearchSuccessResult(List<Student> result) {
		p.remove(studentPanel);
		studentPanel = this.getStudentTablePanel(result);
		this.p.add("联系人列表",studentPanel);
		this.p.add("宿舍列表",roomPanel);
		this.add(p,BorderLayout.WEST);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}
	public void showStudentSearchFalseResult(List<Student> result) {
		p.remove(studentPanel);
		studentPanel = this.getStudentTablePanel(result);
		this.p.add("联系人列表",studentPanel);
		this.p.add("宿舍列表",roomPanel);
		this.add(p,BorderLayout.WEST);
		this.validate();
		JOptionPane.showMessageDialog(this, "查无此人！","错误",JOptionPane.ERROR_MESSAGE);
	}
	public void showRoomSearchResult(List<Room> result) {
		p.remove(roomPanel);
		roomPanel = this.getRoomTablePanel(result);
		this.p.add("联系人列表",studentPanel);
		this.p.add("宿舍列表",roomPanel);
		this.add(p,BorderLayout.WEST);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}
	public void showRoomSearchFalseResult(List<Room> result) {
		p.remove(roomPanel);
		roomPanel = this.getRoomTablePanel(result);
		this.p.add("联系人列表",studentPanel);
		this.p.add("宿舍列表",roomPanel);
		this.add(p,BorderLayout.WEST);
		this.validate();
		JOptionPane.showMessageDialog(this, "查无此人！","错误",JOptionPane.ERROR_MESSAGE);
	}
	
	/**
	 * 菜单栏
	 * @return
	 */
	private JMenuBar getStudentMenuBar() {
		JMenuItem addStudentMenuItem = new JMenuItem("添加联系人");
		JMenuItem addRoomMenuItem = new JMenuItem("添加宿舍");
		JMenuItem listStudentMenuItem = new JMenuItem("通讯录");
		JMenuItem listRoomMenuItem = new JMenuItem("大学宿舍列表");
		addStudentMenuItem.setFont(new Font("黑体", Font.PLAIN, 14));
		addRoomMenuItem.setFont(new Font("黑体", Font.PLAIN, 14));
		listStudentMenuItem.setFont(new Font("黑体", Font.PLAIN, 14));
		listRoomMenuItem.setFont(new Font("黑体", Font.PLAIN, 14));
		addStudentMenuItem.addActionListener(controllerListener);
		addStudentMenuItem.setActionCommand("addDialog");
		addRoomMenuItem.addActionListener(controllerListener);
		addRoomMenuItem.setActionCommand("addRoomDialog");
		listStudentMenuItem.addActionListener(controllerListener);
		listStudentMenuItem.setActionCommand("overviewStudent");
		listRoomMenuItem.addActionListener(controllerListener);
		listRoomMenuItem.setActionCommand("overviewRoom");
		JMenuBar bar = new JMenuBar();
		JMenu saveMenu = new JMenu("新建");
		JMenu viewMenu = new JMenu("总览");
		saveMenu.add(addStudentMenuItem);
		saveMenu.add(addRoomMenuItem);
		viewMenu.add(listStudentMenuItem);
		viewMenu.add(listRoomMenuItem);
		bar.add(saveMenu);
		bar.add(viewMenu);
		saveMenu.setFont(new Font("黑体", Font.PLAIN, 14));
		viewMenu.setFont(new Font("黑体", Font.PLAIN, 14));
		return bar;
	}
	private JToolBar getModifyToolBar() {
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(RIGHT_ALIGNMENT);//设置向右对齐
		JButton searchButton = new JButton("   查询");
		searchButton.setFont(new Font("黑体", Font.BOLD, 16));
		ImageIcon searchIcon = new ImageIcon(ContactsFrame.class.getResource("/查询列表.png"));
		Image img = searchIcon.getImage();

		Image newimg = img.getScaledInstance(30, 26, java.awt.Image.SCALE_SMOOTH);

		searchIcon = new ImageIcon(newimg);  
		searchButton.setIcon(searchIcon);
		searchButton.addActionListener(controllerListener);
		searchButton.setActionCommand("searchDialog");
		toolBar.add(searchButton);	
		toolBar.add(new JToolBar.Separator());
		return toolBar;
	}
	/**
	 * 联系人列表面板
	 * @return
	 */
	private JPanel getStudentTablePanel(List<Student> studentList) {
		tablePanel = new JPanel();
		String titles[] = {"姓名","电话号码"};
		Object data[][] = new Object[studentList.size()][2];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getName();
			data[i][1] = student.getPhoneNumber();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); //表头高度
		table.setRowHeight(table.getRowHeight()+30); //表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
		tablePanel.add(new JScrollPane(table));
		table.setPreferredScrollableViewportSize(new Dimension(250, 400));
		table.repaint();
//		table.addMouseListener(new MouseAdapter() {
//			public void mouseClicked(final MouseEvent e) {
//				selRow = table.getSelectedRow();  //得到选中的行
//				String name = table.getValueAt(selRow, 0).toString().trim();
//				studentId = Long.parseLong(table.getValueAt(selRow, 1).toString());
//				String roomName = (String)table.getValueAt(selRow, 2);
//				String date = table.getValueAt(selRow, 3).toString().trim();
//				String phoneNum = table.getValueAt(selRow, 4).toString().trim();
//				String email = table.getValueAt(selRow, 5).toString().trim();
//				String address = table.getValueAt(selRow, 6).toString().trim();
//				String nativePlace = table.getValueAt(selRow, 7).toString().trim();
//				System.out.println("姓名："+name+";学号："+studentId+";宿舍："+roomName+";生日："+date+";手机号码："+phoneNum+";邮箱："+email+";住址："+address+";老家：" +nativePlace);
//			}
//		});
		return tablePanel;
	}
	private JPanel getRoomTablePanel(List<Room> roomList) {
		tablePanel = new JPanel();
		String titles[] = {"宿舍名", "宿舍号"};
		Object data[][] = new Object[roomList.size()][2];
		Iterator<Room> iter = roomList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Room room = iter.next();
			data[i][0] = room.getRoom_name();
			data[i][1] = room.getRoom_id();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data,titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); //表头高度
		table.setRowHeight(table.getRowHeight()+15); //表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
		table.repaint();
		table.setPreferredScrollableViewportSize(new Dimension(250, 400));
		tablePanel.add(new JScrollPane(table));
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow();  //得到选中的行
				String name = table.getValueAt(selRow, 0).toString().trim();
				roomId = (int)table.getValueAt(selRow, 1);
				System.out.println("宿舍名："+name+";宿舍号："+roomId);
			}
		});
		return tablePanel;
	}
	public ContactsControllerListener getControllerListener() {
		return controllerListener;
	}
	public long getStudentId() {
		return studentId;
	}
	public int getRoomId() {
		return roomId;
	}
	public int getSelRow() {
		return selRow;
	}
}

